{include file="header.html"}
<body class="gr">

<div class="navBar">
	<a class="left" href="javascript:history.back()"><i class="return"></i></a>
	<div class="titleView">购物车</div>
</div>

<div class="cart-index main-padding-top">
	{if count($data)>0}
	<section>
		{foreach from=$data name=s item=s}
		<!--<ul class="tableView">-->
			<section>
				{if $s->shop_id>0}
				<header>
					<a href="?app=shop&act=detail&shop_id={$s->shop_id}">
						<div{if $s->shop_avatar} style="background-image:url({$s->shop_avatar});"{/if}></div>{$s->shop_name}
					</a>
				</header>
				{/if}
				{foreach from=$s->goods name=g item=g}
				<ul class="tableView tableView-light">
					<li>
						<div class="view">
							<div class="right">
								<div class="price">￥{number_format($g->price,2)}</div>
								<div>× <span>{$g->quantity}</span></div>
							</div>
							<div class="tick"><input type="checkbox" name="checkbox" id="tick{$g->id}" value="{$g->id}" /><label for="tick{$g->id}"></label></div>
							<a class="pic" href="wap.php?app=goods&act=detail&goods_id={$g->goods_id}" style="background-image:url({$g->default_pic});"></a>
							<a class="name" href="wap.php?app=goods&act=detail&goods_id={$g->goods_id}">{$g->name}</a>
							<div class="spec">{if $g->spec!=''}规格：{$g->spec}{/if}</div>
							<div class="num">
								<div>
									<input type="hidden" class="cart_id" value="{$g->id}" />
									<input type="hidden" class="goods_id" value="{$g->goods_id}" />
									<input type="hidden" class="spec" value="{$g->spec}" />
									<input type="hidden" class="price" value="{$g->price}" />
									<input type="hidden" class="stocks" value="{$g->stocks}" />
									<a href="javascript:void(0)" class="plus ge-left">+</a>
									<a href="javascript:void(0)" class="minus ge-right">-</a>
									<input type="tel" class="quantity" value="{$g->quantity}" val="{$g->quantity}" />
								</div>
							</div>
						</div>
					</li>
				</ul>
				{/foreach}
			</section>
		<!--</ul>-->
		{/foreach}
	</section>
	<div class="bottomView toolBar ge-top">
		<a class="btn" href="javascript:void(0)">去结算(0)</a>
		<div>总计：<span>￥0.00</span></div>
		<a class="all" href="javascript:void(0)">全选</a>
	</div>
	{else}
	<div class="norecord"><div></div>购物车还是空的哦~</div>
	{/if}
</div>

<div class="footer">
	<a class="ico1" href="wap.php"></a>
	<a class="ico2" href="wap.php?app=category&act=index"></a>
	<a class="ico3" href="wap.php?app=article&act=index"></a>
	<a class="ico4 badge this" href="wap.php?app=cart&act=index"><div>{if $cart_notify>0}<sub><b>{$cart_notify}</b></sub>{/if}</div></a>
	<a class="ico5" href="wap.php?app=member&act=index"></a>
</div>

<form action="wap.php?app=cart&act=jiesuan" method="post">
<input type="hidden" name="goods" id="goods" />
</form>

{include file="footer.html"}
<script>
function setTotal(){
	var total = 0, num = 0;
	$(':checked').each(function(){
		var parent = $(this).parent().parent(), quantity = parent.find('.quantity'), price = parent.find('.num .price');
		total += price.val()*1 * quantity.val()*1;
		num += quantity.val()*1;
	});
	$('.bottomView span').html('￥'+total.numberFormat(2)).priceFont('bigPrice');
	$('.bottomView .btn').html('去结算('+num+')');
}
function setCart(parent, num){
	var isEdit = true, quantity = parent.find('.quantity').val();
	if($.isNumeric(num)){
		isEdit = false;
		quantity = num;
	}
	var goods = { goods:[{ goods_id:parent.find('.goods_id').val(), quantity:quantity, spec:parent.find('.spec').val() }] };
	$.postJSON('api.php?app=cart&act=add_cart', { goods:$.jsonString(goods), edit:isEdit?1:0 }, function(json){
		if(json.error!=0){ $.overloadError(json.msg);return }
	});
}
$(function(){
	$('.pic').loadbackground();
	$('.bottomView span, .right .price').priceFont('bigPrice');
	$(':checkbox').change(function(){
		var checked = 0, unchecked = 0;
		$(':checkbox').each(function(){
			if($(this).is(':checked'))checked++;
			else unchecked++;
		});
		if($(':checkbox').length == checked){
			$('.all').addClass('all-x');
		}else{
			$('.all').removeClass('all-x');
		}
		setTotal();
	});
	$('.all').click(function(){
		var _this = $(this);
		if(_this.hasClass('all-x')){
			_this.removeClass('all-x');
			$(':checkbox').removeProp('checked');
		}else{
			_this.addClass('all-x');
			$(':checkbox').prop('checked', true);
		}
		setTotal();
	});
	$('.minus').click(function(){
		var parent = $(this).parent(), quantity = parent.find('.quantity'), stocks = parent.find('.stocks');
		if(quantity.val()*1 <= 1)return;
		quantity.val(quantity.val()*1-1);
		var view = parent.parent().parent(), num = view.find('.right span');
		num.html(quantity.val());
		setTotal();
		setCart(parent, -1);
	});
	$('.plus').click(function(){
		var parent = $(this).parent(), quantity = parent.find('.quantity'), stocks = parent.find('.stocks');
		if(quantity.val()*1+1 > stocks.val()*1){
			$.overloadError('该商品的库存只剩下'+stocks.val()+'件');
			return;
		}
		quantity.val(quantity.val()*1+1);
		var view = parent.parent().parent(), num = view.find('.right span');
		num.html(quantity.val());
		setTotal();
		setCart(parent, 1);
	});
	$('.quantity').focus(function(){
		var parent = $(this).parent(), quantity = $(this), stocks = parent.find('.stocks');
		if(quantity.val().length && !isNaN(quantity.val()) && quantity.val()*1<=stocks.val()*1)quantity.data('value', quantity.val());
	}).blur(function(){
		var parent = $(this).parent(), quantity = $(this), stocks = parent.find('.stocks');
		if(!quantity.val().length || isNaN(quantity.val())){
			$.overloadError('请输入数量');
			quantity.focus().select();
			return;
		}
		if(quantity.val()*1 > stocks.val()*1){
			$.overloadError('该商品的库存只剩下'+stocks.val()+'件');
			quantity.val(quantity.data('value'));
			quantity.focus().select();
			return;
		}
		var view = parent.parent().parent(), num = view.find('.right span');
		num.html(quantity.val());
		setTotal();
		setCart(parent);
	});
	$('.cart-index .tableView').dragshow({
		title : '<i></i>',
		cls : 'delBtn',
		click : function(row){
			var _this = $(this);
			$.post('api.php?app=cart&act=del_cart', { cart_id:row.find('.cart_id').val() }, function(){
				var section = row.parent().parent();
				if(section.parent().find('section').length==1){ location.href = location.href;return }
				if(section.find('li').length==1){
					section.slideUp(200, function(){ section.remove() });
				}else{
					row.slideUp(200, function(){ row.remove() });
				}
				_this.delay(90).slideUp(200);
				setTimeout(function(){ setTotal() }, 300);
			});
		}
	});
	$('.bottomView .btn').click(function(){
		if(!$.checklogin())return;
		if(!$(':checked').length){
			$.overloadError('请选择需要购买的商品');
			return;
		}
		var goods = [];
		$(':checked').each(function(){
			var parent = $(this).parent().parent();
			goods.push({ goods_id:parent.find('.num .goods_id').val(), quantity:parent.find('.num .quantity').val(), spec:parent.find('.num .spec').val() });
		});
		$('#goods').val($.jsonString(goods));
		$('form').submit();
	});
});
</script>